package com.kyzh.core.pager.weal.coupon

import android.graphics.Color
import android.graphics.drawable.VectorDrawable
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.android.material.appbar.AppBarLayout
import com.gushenge.atools.util.AView
import com.gushenge.atools.util.getStatusBarHeight
import com.gushenge.atools.util.setMargins
import com.gushenge.core.beans.Coupon
import com.gushenge.core.beans.CouponGame
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.activities.MyCouponActivity
import com.kyzh.core.databinding.ItemCouponBinding
import com.kyzh.core.databinding.ItemCouponItemBinding
import com.kyzh.core.utils.setVisibility
import com.kyzh.core.utils.startActivity
import com.kyzh.core.utils.startGameDetailActivity
import com.gushenge.core.toast
import com.kyzh.core.utils.ViewUtils
import kotlinx.android.synthetic.main.activity_coupon.*
import kotlinx.android.synthetic.main.activity_coupon.srlRoot
import kotlinx.android.synthetic.main.fragment_homeothers.*
import org.jetbrains.anko.backgroundColorResource
import org.jetbrains.anko.textColorResource

class CouponActivity : BaseActivity() {


    private val beans = arrayListOf<CouponGame>()
    private val adapter = MainAdapter(R.layout.item_coupon,beans)
    private var p =1
    private var max =2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_coupon)
        initView()
    }

    private fun initView() {
        tvMy.setOnClickListener {
            startActivity<MyCouponActivity>()
        }
        initData(1)
        srlRoot.setOnRefreshListener {
            initData(1)
        }
        close.setOnClickListener {
            finish()
        }
        srlRoot.setEnableLoadMore(true)
        srlRoot.setEnableAutoLoadMore(false)
        srlRoot.setOnLoadMoreListener {
            initData(p)
        }
        rvList.adapter = adapter
        adapter.setEmptyView(R.layout.empty)
        toolbar.setMargins(0,getStatusBarHeight(),0,0)
        appbar.addOnOffsetChangedListener(object : ViewUtils.AppBarLayoutStateChangeListener() {
            override fun onStateChanged(appBarLayout: AppBarLayout, state: State) {
                when (state) {
                    State.EXPANDED -> {
                        AView.setStatusBar(this@CouponActivity, false)
                        (closeImg.drawable as VectorDrawable).setTint(Color.WHITE)
                        tvTitle.text = ""
                        tvTitle.textColorResource = R.color.white
                        tvMy.textColorResource = R.color.white
                        appbar.backgroundColorResource = R.color.bg_f8
                    }

                    State.COLLAPSED -> {
                        AView.setStatusBar(this@CouponActivity, true)
                        (closeImg.drawable as VectorDrawable).setTint(resources.getColor(R.color.font_33))
                        tvTitle.text = getString(R.string.couponCenter)
                        tvTitle.textColorResource = R.color.font_33
                        tvMy.textColorResource = R.color.font_33
                    }
                    State.INTERMEDIATE -> {
                        AView.setStatusBar(this@CouponActivity, true)
                        (closeImg.drawable as VectorDrawable).setTint(resources.getColor(R.color.font_33))
                        tvTitle.text = getString(R.string.couponCenter)
                        tvTitle.textColorResource = R.color.font_33
                        tvMy.textColorResource = R.color.font_33
                    }
                }
            }
        })

    }

    private fun initData(p1:Int){

        WealRequest.coupons(p1) { p ,max,data->
            if (p1==1){
                beans.clear()
            }
            adapter.addData(data)
            this.p = p
            this.max = max
        }

        srlRoot.finishRefresh()
        srlRoot.finishLoadMore()
    }


    private inner class MainAdapter(layout:Int,beans:ArrayList<CouponGame>):BaseQuickAdapter<CouponGame,BaseDataBindingHolder<ItemCouponBinding>>(layout,beans){
        override fun convert(holder: BaseDataBindingHolder<ItemCouponBinding>, item: CouponGame) {
            if(item.name == "通用券"){
                holder.getView<TextView>(R.id.tvDetail).setVisibility(false)
            }
            holder.dataBinding?.data = item
            val adapter = CouponAdapter(R.layout.item_coupon_item,item.list)
            val manager = LinearLayoutManager(context,RecyclerView.HORIZONTAL,false)
            holder.dataBinding?.rvList?.apply {
                layoutManager = manager
                this.adapter = adapter
            }
            holder.dataBinding?.tvDetail?.setOnClickListener {
                startGameDetailActivity(item.gid)
            }
        }
    }

    private inner class CouponAdapter(layout:Int,beans:ArrayList<Coupon>):BaseQuickAdapter<Coupon,BaseDataBindingHolder<ItemCouponItemBinding>>(layout,beans){
        override fun convert(holder: BaseDataBindingHolder<ItemCouponItemBinding>, item: Coupon) {
            holder.dataBinding?.data = item
            holder.dataBinding?.get?.setOnClickListener {
                WealRequest.getCoupon(item.id.toInt()){
                    getString(R.string.getSuccess).toast()
                }
            }
        }
    }
}